package com.knowledgegraph.neo4j.controller;

import com.knowledgegraph.common.Result;
import com.knowledgegraph.neo4j.entity.GraphEntity;
import com.knowledgegraph.neo4j.service.Neo4jEntityService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Neo4j 实体控制器
 */
@RestController
@RequestMapping("/neo4j/entities")
@RequiredArgsConstructor
public class Neo4jEntityController {
    
    private final Neo4jEntityService neo4jEntityService;
    
    /**
     * 创建实体
     */
    @PostMapping
    public Result<GraphEntity> createEntity(@RequestBody GraphEntity entity) {
        GraphEntity created = neo4jEntityService.createEntity(entity);
        return Result.success(created);
    }
    
    /**
     * 更新实体
     */
    @PutMapping("/{id}")
    public Result<GraphEntity> updateEntity(@PathVariable Long id, @RequestBody GraphEntity entity) {
        entity.setId(id);
        GraphEntity updated = neo4jEntityService.updateEntity(entity);
        return Result.success(updated);
    }
    
    /**
     * 删除实体
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteEntity(@PathVariable Long id) {
        neo4jEntityService.deleteEntity(id);
        return Result.success();
    }
    
    /**
     * 根据ID查询实体
     */
    @GetMapping("/{id}")
    public Result<GraphEntity> getEntity(@PathVariable Long id) {
        GraphEntity entity = neo4jEntityService.findById(id);
        return Result.success(entity);
    }
    
    /**
     * 根据知识图谱ID查询实体列表
     */
    @GetMapping("/graph/{graphId}")
    public Result<List<GraphEntity>> getEntitiesByGraphId(@PathVariable Long graphId) {
        List<GraphEntity> entities = neo4jEntityService.findByGraphId(graphId);
        return Result.success(entities);
    }
    
    /**
     * 根据类型查询实体
     */
    @GetMapping("/type/{type}")
    public Result<List<GraphEntity>> getEntitiesByType(@PathVariable String type) {
        List<GraphEntity> entities = neo4jEntityService.findByType(type);
        return Result.success(entities);
    }
    
    /**
     * 根据知识图谱ID和类型查询实体
     */
    @GetMapping("/graph/{graphId}/type/{type}")
    public Result<List<GraphEntity>> getEntitiesByGraphIdAndType(@PathVariable Long graphId, @PathVariable String type) {
        List<GraphEntity> entities = neo4jEntityService.findByGraphIdAndType(graphId, type);
        return Result.success(entities);
    }
    
    /**
     * 搜索实体
     */
    @GetMapping("/search")
    public Result<List<GraphEntity>> searchEntities(@RequestParam Long graphId, @RequestParam String keyword) {
        List<GraphEntity> entities = neo4jEntityService.searchEntities(graphId, keyword);
        return Result.success(entities);
    }
    
    /**
     * 获取实体统计信息
     */
    @GetMapping("/statistics/{graphId}")
    public Result<Map<String, Object>> getEntityStatistics(@PathVariable Long graphId) {
        Map<String, Object> statistics = neo4jEntityService.getEntityStatistics(graphId);
        return Result.success(statistics);
    }
    
    /**
     * 获取实体类型分布
     */
    @GetMapping("/type-distribution/{graphId}")
    public Result<List<Map<String, Object>>> getEntityTypeDistribution(@PathVariable Long graphId) {
        List<Map<String, Object>> distribution = neo4jEntityService.getEntityTypeDistribution(graphId);
        return Result.success(distribution);
    }
    
    /**
     * 查询实体及其关系
     */
    @GetMapping("/{id}/relations")
    public Result<Map<String, Object>> getEntityWithRelations(@PathVariable Long id) {
        Map<String, Object> entityData = neo4jEntityService.getEntityWithRelations(id);
        return Result.success(entityData);
    }
    
    /**
     * 查询实体的邻居节点
     */
    @GetMapping("/{id}/neighbors")
    public Result<List<Map<String, Object>>> getEntityNeighbors(@PathVariable Long id) {
        List<Map<String, Object>> neighbors = neo4jEntityService.getEntityNeighbors(id);
        return Result.success(neighbors);
    }
    
    /**
     * 查询重要实体
     */
    @GetMapping("/important/{graphId}")
    public Result<List<Map<String, Object>>> getImportantEntities(@PathVariable Long graphId, 
                                                                 @RequestParam(defaultValue = "10") Integer limit) {
        List<Map<String, Object>> entities = neo4jEntityService.getImportantEntities(graphId, limit);
        return Result.success(entities);
    }
    
    /**
     * 批量创建实体
     */
    @PostMapping("/batch")
    public Result<List<GraphEntity>> batchCreateEntities(@RequestBody List<GraphEntity> entities) {
        List<GraphEntity> created = neo4jEntityService.batchCreateEntities(entities);
        return Result.success(created);
    }
}